package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pro.mapper.WorkFlowProcessMapper;
import com.qidianit.mes.pro.mapper.WorkFlowProductMapper;
import com.qidianit.mes.pro.service.WorkFlowProcessService;
import com.qidianit.mes.pro.service.WorkFlowProductService;
import com.qidianit.mes.pro.vo.WorkflowProductCategory;
import com.qidianit.mes.pro.vo.WorkflowProductRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: shanzhe
 * @Date: 2025/9/19 - 16:32
 */
@Service
public class WorkFlowProductServiceImpl implements WorkFlowProductService {
    @Autowired
    private WorkFlowProductMapper workFlowProductMapper;


    @Override
    public PageInfo<WorkflowProductRelation> queryProductsByProcessCodeAndPageInteger(Integer page, Integer size, Integer workflowId) {
        PageHelper.startPage(page, size);
        List<WorkflowProductRelation> workflowProductRelationList = workFlowProductMapper.queryProductsByProcessCodeAndPage(page, size, workflowId);
        PageInfo<WorkflowProductRelation> pageInfo = new PageInfo<>(workflowProductRelationList);
        return pageInfo;
    }

    @Override
    public int addWorkFlowProductRelation(WorkflowProductRelation workflowProductRelation) {
        ArrayList<WorkflowProductRelation> workflowProductRelation1 =
                workFlowProductMapper.queryWorkFlowProductRelationByproductCode(workflowProductRelation.getWorkflowId());
        for (WorkflowProductRelation productRelation : workflowProductRelation1) {
            if (workflowProductRelation.getProductCode().equals(productRelation.getProductCode()) ) {
                return -1;
            }
        }
        workflowProductRelation.setProductId(workFlowProductMapper.queryProductByProductCode(workflowProductRelation.getProductCode()).getId());

        return workFlowProductMapper.addWorkFlowProductRelation(workflowProductRelation);
    }

    @Override
    public WorkflowProductCategory queryWorkflowAndProductsAndCategoryById(Integer id) {
        return workFlowProductMapper.queryWorkflowAndProductsAndCategoryById(id);
    }

    @Override
    public int updateWorkFlowProductRelation(WorkflowProductRelation workflowProductRelation) {

        if (workflowProductRelation == null || workflowProductRelation.getId() == null || workflowProductRelation.getWorkflowId() == null) {
            return 0;
        }

        ArrayList<WorkflowProductRelation> workflowProductRelationArrayList =
                workFlowProductMapper.queryWorkFlowProductRelationByproductCode(workflowProductRelation.getWorkflowId());

        for (WorkflowProductRelation productRelation : workflowProductRelationArrayList) {
            if (workflowProductRelation.getProductCode().equals(productRelation.getProductCode())
            && !workflowProductRelation.getId().equals(productRelation.getId())) {
                return -1;
            }
        }
        return workFlowProductMapper.updateWorkFlowProductRelation(workflowProductRelation);
    }

    @Override
    public int deleteWorkFlowProductRelationById(Integer id) {
        return workFlowProductMapper.deleteWorkFlowProductRelationById(id);
    }

    @Override
    public int deleteWorkFlowProductRelationByIds(List<Integer> id) {
        Integer sum = 0;
        for (Integer i : id) {
            sum += workFlowProductMapper.deleteWorkFlowProductRelationById(i);
        }
        if (sum.equals(id.size())) {
            return 0;
        }
        if(sum > 0){
            return -1;
        }
        return 0;
    }
}
